package com.eqdd.yiqidian.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.annotation.api.DealCodeInjector;
import com.eqdd.annotation.datasave.DealCodeClass;
import com.eqdd.yiqidian.FunctionFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.BGABannerAdapter;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.FunModules;
import com.eqdd.yiqidian.bean.rebuild.QiyeImgBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.ui.gongneng.car.CarActivity;
import com.eqdd.yiqidian.ui.gongneng.car.CarRecordListActivity;
import com.eqdd.yiqidian.ui.gongneng.daka.DakaActivity;
import com.eqdd.yiqidian.ui.gongneng.kehu.KehuActivity;
import com.eqdd.yiqidian.ui.gongneng.renshi.RenshiActivity;
import com.eqdd.yiqidian.ui.yiqiyue.YueMainActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.web.WebActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvzhihao on 17-4-8.
 */

public class FunctionFragment extends BaseFragment {

    private SlimAdapterEx<FunModules> slimAdapterEx;
    private FunctionFragmentCustom dataBinding;

    private int[] resIds = {R.mipmap.banenr1, R.mipmap.banner2,
            R.mipmap.banner3};
    private ArrayList<String> tips;
    private ArrayList<String> imgUrls;
    private Object register;
    private SparseArray<Class> models;

    /**
     * classes 所管理的页面
     *
     * @param num 所管理页面红点总数
     */
    @DealCodeClass(classes = {RenshiActivity.class})
    public void unpdateRenshi(int num) {
        slimAdapterEx.getDataItem(0).getChildren().get(0).setNum(num);
        slimAdapterEx.notifyItemChanged(0);
    }

    @Override
    protected void setView() {
        models = new SparseArray<>();
        models.put(0, RenshiActivity.class);
        models.put(1, KehuActivity.class);
        models.put(2, DakaActivity.class);
        models.put(3, CarActivity.class);
        models.put(4, CarRecordListActivity.class);
        models.put(5, YueMainActivity.class);
        OkGo.<HttpResult<List<FunModules>>>post(HttpConfig.BASE_URL + HttpConfig.GET_FUNCTION)
                .params("userGuid", AppConstent.user.getGuid())
                .params("comid", AppConstent.user.getCompanyId())
                .execute(new JsonCallBack<HttpResult<List<FunModules>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FunModules>>> response) {
                        HttpResult<List<FunModules>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                            ((FunctionFragment$$DealCodeInjector) register).update1();
                        }
                    }
                });
    }

    @Override
    protected void initData() {
        register = DealCodeInjector.register(this, RxBus.class);
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_100, new SlimInjector<FunModules>() {
                @Override
                public void onInject(FunModules data, IViewInjector injector) {
                    injector.text(R.id.tv_title, data.getText());
                    RecyclerView recyclerView = (RecyclerView) injector.findViewById(R.id.recyclerView);
                    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
                    recyclerView.addItemDecoration(
                            new GridSpacingItemDecoration(5, 50, false));
                    SlimAdapterEx<FunModules.ChildrenBean> slimAdapterExModule = SlimAdapterEx.create().register(R.layout.grid_item, new SlimInjector<FunModules.ChildrenBean>() {
                        @Override
                        public void onInject(FunModules.ChildrenBean data, IViewInjector injector) {
                            injector.text(R.id.ItemText, data.getText())
                                    .image(R.id.ItemImage, data.getIcon())
                                    .with(R.id.red_dot, view -> {
                                        if (data.getNum() > 0) {
                                            view.setVisibility(View.VISIBLE);
                                        } else {
                                            view.setVisibility(View.INVISIBLE);
                                        }
                                    });
                        }
                    }).attachTo(recyclerView).updateData(data.getChildren());
                    ItemClickSupport.addTo(recyclerView).setOnItemClickListener((rv, p, v) -> {
                        startActivity(models.get(slimAdapterExModule.getDataItem(p).getModule()));
                    });
                }
            }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            initPagerImg();
        }

    }

    private void initPagerImg() {
        dataBinding.bannerViewpager.setAdapter(new BGABannerAdapter(getActivity()));
        dataBinding.bannerViewpager.setData(resIds);
        OkGo.<HttpResult<List<QiyeImgBean>>>post(HttpConfig.BASE_URL + HttpConfig.EQD_IMG_LIST)
                .execute(new JsonCallBack<HttpResult<List<QiyeImgBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<QiyeImgBean>>> response) {
                        HttpResult<List<QiyeImgBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            imgUrls = new ArrayList<>();
                            tips = new ArrayList<>();
                            for (int i = 0; i < httpResult.getItems().size(); i++) {
                                imgUrls.add(httpResult.getItems().get(i).getImageUrl());
                                tips.add(httpResult.getItems().get(i).getTitle());
                            }
                            dataBinding.bannerViewpager.setData(imgUrls, tips);
                            dataBinding.bannerViewpager.setDelegate((banner, itemView, model, position) -> {
                                Bundle bundle = new Bundle();
                                bundle.putString(Config.TITLE, httpResult.getItems().get(position).getTitle());
                                bundle.putString(Config.URL, httpResult.getItems().get(position).getUrl());
                                startActivity(WebActivity.class, bundle);
                            });

                        }
                    }
                });
    }

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_function, null, false);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        DealCodeInjector.unregister(this, RxBus.class, register);
        super.onDestroy();
    }
}